//
//  DictionaryViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/9.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation
import UIKit

class DictionaryViewController : BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titles = ["statement","check","edit"]
        self.tableView?.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        switch indexPath.row{
        case 0:
            statement()
        case 1:
            check()
        case 2:
            edit()
        default:
            print("no more methods")
        }
    }
    
    fileprivate func statement() {
        let dic1 : Dictionary<String,String> = ["hello" : "world"]
        print(dic1)
        
        let 赵日天 : Dictionary<String,Bool> = ["1":true,"0":false]
        print(赵日天)
        
        let dic2  = [1:1,"2":2,"3":false] as [AnyHashable : Any]
        print(dic2)
        
        let dic3 : Dictionary<String,String> = Dictionary.init(minimumCapacity: 0)
        dic3.isEmpty ? print("This dictionary is empty") : print("This dictionary is not empty")
        
        let dic4 : Dictionary = ["1":["1","2"]]
        print(dic4)
    }
    
    fileprivate func check() {
        var dic = ["1":"1","2":"2","3":"3"]
        print("count = " + String(dic.count))
        
        print(dic["2"])
        
        if "2" == dic["2"] {
            print("This value is equal to 2")
        }
        
        for (key,value) in dic {
            print("key : " + key)
            print("value : " + value)
        }
        
        for key in dic.keys {
            print("subKey is " + key)
        }
        
        for value in dic.values {
            print("subValue is " + value)
        }
    }
    
    fileprivate func edit() {
        var dic : Dictionary = ["1":"1","2":"2","3":3] as [String : Any]
        
        dic["1"] = "hello"
        print(dic)
        
        dic.removeValue(forKey: "2")
        print(dic)
        
        dic["4"] = "4"
        print(dic)
        
        let array : Array = ["1","2"]
        dic["5"] = array
        print(dic)
    }
}
